﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using SystemMonitor.Classes.Base;
using System.ComponentModel;
using System.Threading;
using System.Management;
using System.Windows;

namespace SystemMonitor.Classes.Models
{
    public class SystemMonitorManager
    {
        private static SystemMonitorManager _instance = null;
        private static readonly object _threadSafetyLock = new object();
        private PerformanceCounter[] performanceCounter;
        private PerformanceCounter proccesorCounter;

        public static SystemMonitorManager Instance
        {
            get
            {
                lock (_threadSafetyLock)
                {
                    if (_instance == null)
                        _instance = new SystemMonitorManager();
                    return _instance;
                }
            }
        }

        public SystemMonitorManager()
        {
            int coreCount = this.ProccesorCount();
            performanceCounter = new PerformanceCounter[coreCount];
            for (int i = 0; i < coreCount; i++)
            {
                performanceCounter[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString(), true);
                performanceCounter[i].NextValue(); 
            }
            proccesorCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            proccesorCounter.NextValue();
        }

        public Int64 TotalPhysicalMemory()
        {
            return PerformanceInfo.GetTotalMemoryInMiB();
        }

        public Int64 UsedPhysicalMemory()
        {
            return PerformanceInfo.GetTotalMemoryInMiB() - PerformanceInfo.GetPhysicalAvailableMemoryInMiB();
        }

        public Int64 AvailablePhysicalMemory()
        {
            return PerformanceInfo.GetPhysicalAvailableMemoryInMiB();
        }

        public int ProccesorCount()
        {
            return Environment.ProcessorCount;
        }

        public float ProccesorThreadUsage(int threadNumber)
        {
            if (threadNumber >= this.ProccesorCount())
            {
                throw new Exception("Incorrect core number");
            }
            return performanceCounter[threadNumber].NextValue();
        }

        public float ProccesorUsage()
        {
            return proccesorCounter.NextValue();
        }

        public string ProccesorType()
        {
            ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_Processor");
            foreach (ManagementObject share in searcher.Get())
            {
                foreach (PropertyData PC in share.Properties)
                {
                    Utils.Log("{0} - {1}", PC.Name, PC.Value);
                }
            }
            /*
                if (share.Properties.Count <= 0)
                {
                    throw new Exception("No information available");
                }
                Utils.Log("{0}", share.Properties["Name"]);
             */

            searcher = new ManagementObjectSearcher(@"root\WMI", "SELECT * FROM MSAcpi_ThermalZoneTemperature");
            foreach (ManagementObject obj in searcher.Get())
            {
                foreach (PropertyData PC in obj.Properties)
                {
                    Utils.Log("{0} - {1}", PC.Name, PC.Value);
                }

                Double temp = Convert.ToDouble(obj["CriticalTripPoint"].ToString());
                temp = temp / 10.0 - 273.15;
                Utils.Log("{0}", temp);
                
            }

            return string.Empty;
        }
    }
 
}
